﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

//using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;

using Baosight.ZNH.ZMCA.UI.Controls;
using Baosight.ZNH.ZMCA.UI.Utils;

namespace Baosight.ZNH.ZMCA.UI.Controls
{
    public partial class EngineeringPicker : EFView
    {
        /// <summary>
        /// 是否显示工程选择框
        /// </summary>
        public Visibility ShowEngineeringName { get; set; }
        /// <summary>
        /// 是否显示打钻工程选项
        /// </summary>
        public Visibility ShowTypeHole { get; set; }
        public EngineeringPicker()
        {
            InitializeComponent();
            ShowEngineeringName = System.Windows.Visibility.Visible;
            ShowTypeHole = System.Windows.Visibility.Visible;
            this.fdEngineeringOrg.DropDownClosed += new DropDownMultiCheckBox.MyEventHandler(fdEngineeringOrg_DropDownClosed);
            this.Loaded +=new RoutedEventHandler(EngineeringPicker_Loaded);
        }

        #region 对外接口

        /// <summary>
        /// 获取所选工程类型
        /// </summary>
        /// <returns></returns>
        public string GetEngineeringType()
        {
            return this.fdEngineeringType.Value.ToString().Trim();
        }

        /// <summary>
        /// 获取所选工程单位ID
        /// </summary>
        /// <returns></returns>
        public IList<EIRow> GetEngineeringOrg()
        {
            return this.fdEngineeringOrg.GetCheckedItems();
        }

        /// <summary>
        /// 获取所选工程ID
        /// </summary>
        /// <returns></returns>
        public IList<EIRow> GetEngineeringName()
        {
            return this.fdEngineeringName.GetCheckedItems(); 
        }

        /// <summary>
        /// 判断工程单位是否全选
        /// </summary>
        /// <returns></returns>
        public bool IsCheckedAllOrg()
        {
            return this.fdEngineeringOrg.IsAllChecked();
        }

        /// <summary>
        /// 判断工程是否全选
        /// </summary>
        /// <returns></returns>
        public bool IsCheckedAllEngineering()
        {
            return this.fdEngineeringName.IsAllChecked();
        }

        /// <summary>
        /// 设置工程下拉框是否显示
        /// </summary>
        /// <param name="IsShow"></param>
        public void ShowEngineering(bool IsShow)
        {
            //this.fdEngineeringName.IsShow(IsShow);
            if (!IsShow)
            {
                fdEngineeringName.Visibility = this.nameLabel.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                fdEngineeringName.Visibility = this.nameLabel.Visibility = System.Windows.Visibility.Visible;
            }
        }

        /// <summary>
        /// 设置选中工程单位的全部分析项时“全部”勾选框是否勾选上
        /// </summary>
        /// <param name="flag"></param>
        public void SetEngineeringOrgAllItem(bool flag)
        {
            this.fdEngineeringOrg.SetAllCheckBoxChecked(flag);
        }

        /// <summary>
        /// 设置选中工程的全部分析项时“全部”勾选框是否勾选上
        /// </summary>
        /// <param name="flag"></param>
        public void SetEngineeringNameAllItem(bool flag)
        {
            this.fdEngineeringName.SetAllCheckBoxChecked(flag);
        }

        #endregion

        #region 初始化控件
        /// <summary>
        /// 初始化，页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EngineeringPicker_Loaded(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("method", "initLoad");
            eiInfo.Set("service", "ZMCA20");
            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, InitLoadDoWorkCompleted);
        }

        /// <summary>
        /// 返回数据，绑定数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InitLoadDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                EFWindow.Alert("数据库查询数据错误，请检查服务器/数据库连接！");
                return;
            }
            else
            {
                //将打钻工程从工程类型中去掉
                if (Visibility.Collapsed == ShowTypeHole)
                {
                    e.EIInfo.GetBlock("cmes.ca.drillrelatetype").Rows.RemoveAt(e.EIInfo.GetBlock("cmes.ca.drillrelatetype").Rows.Count - 1);
                }
                this.UpdateData(e.EIInfo);

                //手动绑定数据
                EIInfo outInfo = e.EIInfo;
                EIBlock orgBlock = outInfo.GetBlock("cmes.ca.department");
                EIBlock prjBlock = outInfo.GetBlock("cmes.ca.engineering");

                if (orgBlock == null || prjBlock == null)
                {
                    return;
                }

                this.fdEngineeringOrg.BindItemsToGrid(orgBlock.GetRows(), "orgName");   //初始化控件时显示全部数据
                this.fdEngineeringName.BindItemsToGrid(prjBlock.GetRows(), "fdName");
                fdEngineeringType.Value = "0";
            }
        }

        #endregion

        #region 控件事件响应

        /// <summary>
        /// 工程类型选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdEngineeringType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            this.fdEngineeringName.SetContent("全部");
            this.fdEngineeringOrg.SetContent("全部");
            string fdType = this.fdEngineeringType.Value.ToString().Trim();

            EIBlock block = new EIBlock("queryBlock");
            Common.AddColumnToBlock(block, "fdType");

            if (!fdType.Trim().Equals(""))
            {
                EIRow row = new EIRow();
                nameLabel.Visibility = ShowEngineeringName;
                fdEngineeringName.Visibility = ShowEngineeringName;
                if (fdType.Trim().Equals("3"))      //开拓工程
                {
                    fdType = "1";
                    Common.AddColumnToBlock(block, "fdNo");
                    row.Add("fdNo", "0");
                }
                else if (fdType.Trim().Equals("2"))     //钻孔工程
                {
                    nameLabel.Visibility = Visibility.Collapsed;
                    fdEngineeringName.Visibility = Visibility.Collapsed;
                }

                row.Add("fdType", fdType);
                block.AddRow(row);
            }

            QueryPrjOrg(block);
        }

        /// <summary>
        /// 工程单位选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdEngineeringOrg_DropDownClosed(object sender, object e)
        {
            this.fdEngineeringName.SetContent("全部");
            InitQueryNameByOrg(); 
        }

        #endregion

        #region 控件之间关联查询

        /// <summary>
        /// 通过选择的工程单位查询工程名称
        /// </summary>
        private void InitQueryNameByOrg()
        {
            IList<EIRow> list = this.fdEngineeringOrg.GetCheckedItems();

            EIBlock block = new EIBlock("queryBlock");
            Common.AddColumnToBlock(block, "fdDepartmentId");
            Common.AddColumnToBlock(block, "fdType");

            EIRow row = new EIRow();
            JointQueryBlock(row, list, "orgCode", "fdDepartmentId");

            string fdType = this.fdEngineeringType.Value.ToString().Trim();
            if (!fdType.Trim().Equals(""))
            {
                if (fdType.Trim().Equals("3"))
                {
                    fdType = "1";
                    Common.AddColumnToBlock(block, "fdNo");
                    row.Add("fdNo", "0");
                }

                row.Add("fdType", fdType);
                block.AddRow(row);
            }

            QueryPrjName(block);
        }

        /// <summary>
        /// 工程单位查询
        /// </summary>
        /// <param name="block"></param>
        private void QueryPrjOrg(EIBlock block)
        {
            EIInfo inInfo = new EIInfo();

            inInfo.AddBlock(block);
            inInfo.Set("method", "queryProjectOrg");
            inInfo.Set("service", "ZMCA20");
            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(inInfo, QueryPrjOrgDoWorkCompleted);
        }

        /// <summary>
        /// 工程单位查询结果回调事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryPrjOrgDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                return;
            }
            else
            {
                EIBlock block = e.EIInfo.GetBlock("org");
                this.fdEngineeringOrg.BindItemsToGrid(block.GetRows(), "orgName");

                this.InitQueryNameByOrg();
            }
        }

        /// <summary>
        /// 工程单位查询
        /// </summary>
        /// <param name="block"></param>
        private void QueryPrjName(EIBlock block)
        {
            EIInfo inInfo = new EIInfo();

            inInfo.AddBlock(block);
            inInfo.Set("method", "queryProjectName");
            inInfo.Set("service", "ZMCA20");
            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(inInfo, QueryPrjNameDoWorkCompleted);
        }

        /// <summary>
        /// 工程名称查询结果回调事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryPrjNameDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                return;
            }
            else
            {
                EIBlock block = e.EIInfo.GetBlock("eng");
                this.fdEngineeringName.BindItemsToGrid(block.GetRows(), "fdName");
            }
        }

        /// <summary>
        /// 拼接查询字段
        /// </summary>
        /// <param name="row"></param>
        /// <param name="list"></param>
        /// <param name="sourIndex"></param>
        /// <param name="destIndex"></param>
        private void JointQueryBlock(EIRow row, IList<EIRow> list, string sourIndex, string destIndex)
        {
            string fdIndex = "''";

            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (i == 0)
                    {
                        if ((list[0][sourIndex] == null) || (list[0][sourIndex].ToString().Trim().Equals("")))
                        {
                            continue;
                        }
                        else
                        {
                            fdIndex = "'" + list[0][sourIndex].ToString().Trim() + "'";
                        }
                    }
                    else
                    {
                        if ((list[i][sourIndex] == null) || (list[i][sourIndex].ToString().Trim().Equals("")))
                        {
                            continue;
                        }
                        else
                        {
                            fdIndex = fdIndex + ", '" + list[i][sourIndex].ToString().Trim() + "'";
                        }
                    }
                }
            }

            row.Add(destIndex, fdIndex);
        }

        #endregion



    }
}
